/**
Metamorfose is an interactive ecosystem, which uses computer vision
and sound analysis to define its basic rules.
Copyright (C) 2008  laboratorio (info@laboratorio.us)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import tuio.*;
import noc.*;
import java.util.Hashtable;
import processing.video.*;
import pitaru.sonia_v2_9.*;
import ddf.minim.*;

import processing.video.*;

TuioClient tuioClient;
PFont font;
Ecosystem ecosystem;
Logger logg;
Animation[] animations;
Animation amebaOne, amebaTwo, amebaTree;
PImage bgTop, bgBottom;
Movie bgMiddle;
AudioPlayer bgSound;
SampleLib sampleLib;

MovieMaker mm;

void setup()
{
  size(1024, 768);
  noStroke();
  fill(0);
  frameRate(30);
  imageMode(CENTER);
  smooth();

  bgMiddle = new Movie(this, "sprites/bg_middle/bg_middle.mov");
  bgMiddle.loop();
  animations = new Animation[3];
  amebaOne = new Animation("ameba_01", 25);
  amebaTwo = new Animation("ameba_02", 24);
  amebaTree = new Animation("ameba_03", 24);
  animations[0] = amebaOne;
  animations[1] = amebaTwo;
  animations[2] = amebaTree;
  tuioClient  = new TuioClient(this); 
  ecosystem = new Ecosystem();
  Sonia.start(this);
  LiveInput.start(32);  
  Minim.start(this);
  bgSound = Minim.loadFile("data/sample/hecq/bg/tudo.aiff", 1024);
  bgSound.play();
  bgSound.loop();
  sampleLib = new SampleLib("data/sample/hecq/onShot/", 25);
  //logg = new Logger(this);
  /*mm = new MovieMaker(this, width, height, "drawing.mov",
                       10, MovieMaker.ANIMATION, MovieMaker.HIGH);*/
}


void draw()
{
  background(255);
  image(bgMiddle, 0, 0);
  ecosystem.cicle();
  handleUserInteractions();
  
  if (keyPressed) {
    ecosystem.launchParasites();
    ecosystem.reload();      
    /*if (key == ' ') {
      mm.finish();  // Finish the movie if space bar is pressed!
    } else {
      ecosystem.launchParasites();
      ecosystem.reload();      
    }*/
  }
  //mm.addFrame();
}

void handleUserInteractions()
{
  LiveInput.getSpectrum();
  int maxVal = 8500;
  for ( int i = 0; i < LiveInput.spectrum.length; i++) {
    int val = int(LiveInput.spectrum[i]);
    if (val > maxVal) {
      ecosystem.launchParasites();
      ecosystem.reload();      
    }
  }
}

void addTuioObject(TuioObject tuio) {
  ecosystem.add(tuio.getSessionID(), 
                tuio.getScreenX(width), 
                tuio.getScreenY(height));
  /*logg.record(tuio.getSessionID(), 
              tuio.getFiducialID(),
              tuio.getScreenX(width),
              tuio.getScreenY(width),
              tuio.getAngle());*/
}

void removeTuioObject(TuioObject tuio) {
  ecosystem.kill(tuio.getSessionID());
}

void updateTuioObject (TuioObject tuio) {
  ecosystem.update(tuio.getSessionID(), 
                  tuio.getScreenX(width), tuio.getScreenY(height), tuio.getAngle());
  /*logg.record(tuio.getSessionID(), 
              tuio.getFiducialID(),
              tuio.getScreenX(width),
              tuio.getScreenY(width),
              tuio.getAngle());*/
}

void refresh(long timestamp) { }

void movieEvent(Movie m) {
  m.read();
}